//
//  CustomViewController.m
//  StickyDemo
//
//  Created by kckj on 2021/7/5.
//

#import "CustomViewController.h"
#import "CustomTableViewCell.h"
#import "TabController.h"
/** 屏幕宽 */
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

@interface CustomViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) CGRect OriginFrame;
@property (nonatomic, assign) BOOL isDrag;

@property (nonatomic, strong) TabController *oneView;
@property (nonatomic, strong) TabController *twoView;
@property (nonatomic, strong) TabController *thereView;
@end

@implementation CustomViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.oneView = [[TabController alloc] init];
    [self.oneView.view setBackgroundColor:[UIColor redColor]];
    
    self.twoView = [[TabController alloc] init];
    [self.twoView.view setBackgroundColor:[UIColor greenColor]];
    
    self.thereView = [[TabController alloc] init];
    [self.thereView.view setBackgroundColor:[UIColor blueColor]];
    
    [self.tableView reloadData];
}
#pragma mark - ScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    self.isDrag = YES;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    
    CGFloat y = scrollView.contentOffset.y;
    if (y > 0) {
        return;
    }
    if (y < -20) {
        self.isDrag = NO;
        if (self.didDownREfresh) {
            self.didDownREfresh();
        }
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat y = scrollView.contentOffset.y;
    if (!self.isDrag) {
        if (y > 0) {
            if (self.didDownScroll) {
                self.didDownScroll(y);
            }
        }
        return;
    }
    if (self.didDownScroll) {
        self.didDownScroll(y);
    }
}
#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return SCREEN_WIDTH+60;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(CustomTableViewCell.class)];
    [cell cellScrollViewAddChildViews:@[self.oneView,self.twoView,self.thereView]];
    return cell;
}
#pragma mark - Getter
- (UITableView *)tableView  {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) style:UITableViewStyleGrouped];
        [self.view addSubview:_tableView];
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
        UILabel *label = [[UILabel alloc] init];
        [view addSubview:label];
        label.frame = CGRectMake(0, 88, SCREEN_WIDTH, 100);
        [label setText:@"其实到这里会发现主要是通过设置两个scrollView的偏移量来解决这个手势冲突的问题。因为代码已经表达的很清楚，不太擅长表达，这里只是大概描述一下使用场景。具体的实现方式已经写成了一个demo，里面也有详细的注释，可以前去下载："];
        
        [label setNumberOfLines:0];
        [label setTextColor:[UIColor whiteColor]];
        [_tableView setTableHeaderView:view];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView setBackgroundColor:[UIColor clearColor]];
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass(CustomTableViewCell.class) bundle:nil] forCellReuseIdentifier:NSStringFromClass(CustomTableViewCell.class)];
    }
    return _tableView;
}
- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    self.tableView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));
}
@end
